﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.Shop
{
    public partial class message : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(message), Page);
        }

        [AjaxPro.AjaxMethod]
        public string OnSaveMessage(string json,string shop)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            if (CommonBll.IsLegitimateShopId(shop))
            {
                try
                {
                    var dicProj = JsonHelper.JSONToObject<Dictionary<string, object>>(json);
                    dicProj.Add("Client_Id", CommonBll.GetTheRealId(shop));
                    var content = Server.HtmlEncode(dicProj["Message_Note"].ToString()).Replace("/", "");
                    dicProj.Remove("Message_Note");
                    dicProj.Add("Message_Note", content);
                    var userInfo = WebMasterCookie.GetCookie();
                    if (userInfo != null)
                    {
                        dicProj.Add("Senter_Id", userInfo.Id);

                        dicProj.Add("Message_Date", DateTime.Now);
                        MessageInfoBll.InsertMessageInfo(dicProj, CommonConst.DB_Name);
                        jsonResult.Add("isreload", true);
                        jsonResult.Add("msg", "留言成功！");
                    }
                    else
                    {
                        jsonResult.Add("isreload", false);
                        jsonResult.Add("msg", "请登录后留言！");
                    }
                }
                catch (Exception ex)
                {
                    jsonResult.Clear();
                    jsonResult.Add("isreload", false);
                    jsonResult.Add("msg", ex.Message);
                }
                

            }
            else
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", "0");
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //验证验证码是否正确
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string VerificationCode(string code)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            string verCode = Session["VerificationCode"] == null ? "" : Session["VerificationCode"].ToString();
            if (verCode.ToLower() == code.ToLower())
            {
                jsonResult.Add("isreload", true);
            }
            else
            {
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //判断用户是否已经登录
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public bool IsLogin()
        {
            var userInfo = WebMasterCookie.GetCookie();
            if (userInfo!=null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string UserLogin(string account, string pwd)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                //判断用户是否已经登录
                var userInfo = WebMasterCookie.GetCookie();
                if (userInfo != null)
                {
                    jsonResult.Add("isreload", true);
                }
                else
                {
                    DataTable table = ClientInfoBll.ClientLogin(CommonBll.InputText(account, 50),
                                                                EncryptHelper.EncryptString(pwd),  CommonConst.DB_Name);
                    if (table.Rows.Count > 0)
                    {
                        //判断当天是否已经登录过
                        if (table.Rows[0]["LastLoginTime"] == DBNull.Value ||
                            (Convert.ToDateTime(table.Rows[0]["LastLoginTime"].ToString()) - DateTime.Now).Days != 0)
                        {
                            //登录时积分+5并更新等级
                            ClientInfoBll.UpdateClientPoints(table.Rows[0]["Client_Id"].ToString(), CommonConst.gLoginPoints, "+",  CommonConst.DB_Name);
                            //更新最后登录时间
                            ClientInfoBll.UpateLastLoginTime(table.Rows[0]["Client_Id"].ToString(),  CommonConst.DB_Name);
                        }
                        

                        jsonResult.Add("isreload", true);
                        var info = new UserInfo
                        {
                            Email = table.Rows[0]["User_Email"] == DBNull.Value ? "" : table.Rows[0]["User_Email"].ToString(),
                            Id = table.Rows[0]["Client_Id"].ToString(),
                            RelName = table.Rows[0]["Client_Name"] == DBNull.Value ? "" : table.Rows[0]["Client_Name"].ToString(),
                            Status = table.Rows[0]["User_Status"].ToString(),
                            UName = table.Rows[0]["Client_Account"].ToString()
                        };
                        info.Computername = HttpContext.Current.Application["computername"].ToString();
                        info.Computerip = HttpContext.Current.Application["computerip"].ToString();
                        WebMasterCookie.AddCookie(info);

                        Dictionary<string, object> dicLoginLog = new Dictionary<string, object>();
                        dicLoginLog.Add("user_id", info.Id);
                        dicLoginLog.Add("computername", info.Computername);
                        dicLoginLog.Add("computerip", info.Computerip);
                        LogLoginBll.AddLoginLog(dicLoginLog, CommonConst.DB_Name);
                    }
                    else
                    {
                        jsonResult.Clear();
                        jsonResult.Add("isreload", false);
                        jsonResult.Add("msg", "用户名或密码错误！");
                    }
                }

            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg", ex.Message);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";

        }
    }
}
